package co.yixiang.modules.quartz.service.impl;

import co.yixiang.modules.quartz.service.CrawlerService;
import co.yixiang.modules.shop.domain.YxProductBase;
import co.yixiang.modules.shop.domain.YxProductStockOut;
import co.yixiang.modules.shop.service.ProductStockOutService;
import co.yixiang.modules.shop.service.YxProductBaseService;
import co.yixiang.modules.shop.service.dto.YxProductBaseQueryCriteria;
import co.yixiang.modules.shop.service.dto.YxProductStockOutDTO;
import co.yixiang.modules.shop.service.mapper.YxProductStockOutMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author frq
 * @date 2020-06-09
 */
@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS, readOnly = true)
public class CrawlerServiceImpl implements CrawlerService {
    @Autowired
    private YxProductBaseService yxProductBaseService;

    @Autowired
    private ProductStockOutService productStockOutService;
    /**
     * 奥买家爬虫服务
     */
    @Autowired
    private AMJPcProductParser aMJPcProductParser;
    /**
     * 京东爬虫服务
     */
    @Autowired
    private JDPcProductParser jDPcProductParser;
    /**
     * 苏宁爬虫服务
     */
    @Autowired
    private SNPcProductParser sNPcProductParser;
    /**
     * 天猫爬虫服务
     */
    @Autowired
    private TMPcProductParser tMPcProductParser;

    @Autowired
    private YxProductStockOutMapper yxProductStockOutMapper;

    /**
     * 抓取商城数据
     * @param number 抓取记录数
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int fetchingData(int number) {
        //查询未抓取的记录
        YxProductBaseQueryCriteria criteria = new YxProductBaseQueryCriteria();
        criteria.setStatus(0);
        int result = 0;
        List<YxProductBase> listBase = yxProductBaseService.queryAll(criteria);
        if (listBase != null && listBase.size() > 0) {
            List<YxProductStockOutDTO> list = new ArrayList<>();
            for (YxProductBase productBase : listBase) {
                //抓取京东商城数据
                List<YxProductStockOutDTO> listJD = jDPcProductParser.parserList(productBase.getName(), number,productBase.getId());
                list.addAll(listJD);
                //抓取苏宁商城数据
                List<YxProductStockOutDTO> listSN = sNPcProductParser.parserList(productBase.getName(), number,productBase.getId());
                list.addAll(listSN);
                //抓取天猫商城数据
                List<YxProductStockOutDTO> listTM = tMPcProductParser.parserList(productBase.getName(), number,productBase.getId());
                list.addAll(listTM);
                //抓取欧买家商城数据
                List<YxProductStockOutDTO> listAMJ = aMJPcProductParser.parserList(productBase.getName(), number,productBase.getId());
                list.addAll(listAMJ);
                //抓取完成，修改状态
                productBase.setStatus(1);
                yxProductBaseService.update(productBase);
            }
            //抓取完成，入库
            if(list!=null&&list.size()>0){
                for (YxProductStockOutDTO stockOut : list) {
                    YxProductStockOut yxProductStockOut =yxProductStockOutMapper.toEntity(stockOut);
                    yxProductStockOut.setAddTime(new Date());
                    yxProductStockOut.setIsDel(0);
                    productStockOutService.save(yxProductStockOut);
                }
            }
            result=1;
        }
        return result;
    }
}
